﻿using CKS2320.Common.Model;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;

namespace CKS2320.Common.Utils
{
    /// <summary>
    /// Model操作类
    /// </summary>
    public class ModelUtils
    {
        /// <summary>
        /// 返回匹配的对象
        /// </summary>
        /// <param name="obj"></param>
        /// <param name="property"></param>
        /// <returns></returns>
        public static object GetPropertyValue(object obj, string property)
        {
            PropertyInfo propertyInfo = obj.GetType().GetProperty(property);
            return propertyInfo.GetValue(obj, null);
        }
        /// <summary>
        /// 根据枚举值获取枚举注释
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="key"></param>
        /// <param name="model"></param>
        /// <returns></returns>
        public static string GetEnumDescriptionByKey<T>(string key, object model)
        {
            PropertyInfo[] properlist = model.GetType().GetProperties();
            foreach (PropertyInfo info in properlist)
            {
                if (info.Name == key)
                {
                    PropertyDescriptor pd = TypeDescriptor.GetProperties(model.GetType())[info.Name];
                    DescriptionAttribute description = pd == null ? null : pd?.Attributes[typeof(DescriptionAttribute)] as DescriptionAttribute;
                    if (!string.IsNullOrEmpty(description?.Description))
                        return description.Description;
                }
            }
            return null;
        }
        /// <summary>
        /// 接口返回结果拼接
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="modelList"></param>
        /// <returns></returns>
        public static List<ReturnKeyValueList> ReturnTransfor<T>(List<T> modelList)
        {
            List<ReturnKeyValueList> returnList = new List<ReturnKeyValueList>();
            foreach (var model in modelList)
            {
                var returnInfo = new ReturnKeyValueList();
                returnInfo.values = new List<ReturnKeyValue>();
                PropertyInfo[] properlist = model.GetType().GetProperties();
                foreach (PropertyInfo info in properlist)
                {
                    if (info != null)
                    {
                        var kvInfo = new ReturnKeyValue()
                        {
                            Key = info.Name,
                            Value = info.GetValue(model, null)?.ToString(),
                        };
                        PropertyDescriptor pd = TypeDescriptor.GetProperties(model.GetType())[info.Name];
                        DisplayNameAttribute displayName = pd == null ? null : pd?.Attributes[typeof(DisplayNameAttribute)] as DisplayNameAttribute;
                        if (!string.IsNullOrEmpty(displayName?.DisplayName))
                        {
                            kvInfo.Display = true;
                            kvInfo.Name = displayName?.DisplayName;
                        }
                        else
                        {
                            kvInfo.Display = false;
                            kvInfo.Name = info.Name;
                        }
                        returnInfo.values.Add(kvInfo);
                    }
                }
                returnList.Add(returnInfo);
            }
            return returnList;
        }
    }
}
